package com.gg.zjj.service.impl;

import com.gg.common.mapper.*;
import com.gg.common.pojo.*;
import com.gg.zjj.pojo.MProcedureModuleView;
import com.gg.zjj.pojo.SPayDetailsView;
import com.gg.zjj.pojo.SubmitOperationView;
import com.gg.zjj.service.MManufactureService;
import com.gg.zjj.util.Result;
import com.gg.hj.util.INumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author koco
 * 2022/10/19
 * 15:52
 */
@Service
public class MManufactureServiceImpl implements MManufactureService {
    @Resource
    private MManufactureMapper manufactureMapper;

    @Resource
    private MProcedureMapper procedureMapper;

    @Resource
    private MProcedureModuleMapper moduleMapper;
    @Resource
    private MProcedureIngMapper procedureIngMapper;

    @Resource
    private MProcedureModuleIngMapper moduleIngMapper;
    @Resource
    private SPayMapper payMapper;

    @Resource
    private SPayDetailsMapper detailsMapper;

    @Resource
    private INumber nmber;


    @Override
    public List<MManufacture> queryAllPendingProductionDispatchOrdersService() {
        return manufactureMapper.queryAllPendingProductionDispatchOrdersMapper();
    }

    @Override
    public MManufacture queryPendingProductionDispatchOrders(String manufactureId) {
        MManufacture mManufacture = manufactureMapper.queryByManufactureId(manufactureId);
        for (int i = 0; i < mManufacture.getProductionProcesses().size(); i++) {
            Integer id = mManufacture.getProductionProcesses().get(i).getId();
            List<MProcedureModule> productionMaterial=moduleMapper.queryModuleList(id);
            mManufacture.getProductionProcesses().get(i).setProductionMaterial(productionMaterial);
        }
        return mManufacture;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result examinationPassed(MProcedureModuleView view) {
        view.setReviewTime(view.getReviewTime().replace("T", " "));
        manufactureMapper.approvedByDispatchedWorkers(view);
        SPay pay = new SPay();
        int amountSum = 0;
        int costPriceSum = 0;
        try {
            pay.setPayId(nmber.number("402"));
            pay.setStorer(view.getWorkOrderMaker());
            pay.setReason("C002-1");
            pay.setReasonexact(view.getRemark());
            pay.setRegister(view.getReviewer());
            pay.setRegisterTime(view.getReviewTime().replace("T", " "));
            pay.setCheckTag("S001-1");
            pay.setStoreTag("K002-1");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        for (MProcedureModule module : view.getProductionMaterial()) {
            amountSum += module.getAmount();
            costPriceSum += module.getCostPrice();
        }
        pay.setAmountSum(amountSum);
        pay.setCostPriceSum(costPriceSum);
        payMapper.insertSelective(pay);
        List<SPayDetails> list = new ArrayList<>();
        for (MProcedureModule module : view.getProductionMaterial()) {
            SPayDetails details = new SPayDetails();
            details.setParentId(pay.getId());
            details.setProductId(module.getProductId());
            details.setProductName(module.getProductName());
            details.setAmount(module.getAmount());
            details.setCostPrice(module.getCostPrice());
            details.setSubtotal(module.getSubtotal());
            details.setPayTag("K002-1");
            list.add(details);
        }
        SPayDetailsView detailsView = new SPayDetailsView(list);
        detailsMapper.bulkInsertIntoInventorySchedule(detailsView);
        return Result.success();
    }

    @Override
    public Result productionDispatcherRefused(String workOrderNumber) {
        manufactureMapper.productionDispatcherRefused(workOrderNumber);
        return Result.success();
    }

    @Override
    public List<MManufacture> queryAllProductionSummaries() {
        return manufactureMapper.queryAllProductionSummaries();
    }

    @Override
    public List<MManufacture> queryProductionDispatchOrdersToBeRegistered() {
        return manufactureMapper.queryProductionDispatchOrdersToBeRegistered();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result submitMaterialProductionService(MProcedureIng mProcedureIng) {
        try {
//            MManufacture manufacture = new MManufacture();
//            double realModuleCostPriceSum = 0;
//            double realLabourCostPriceSum = mProcedureIng.getSubtotal();
//            for (MProcedureModuleIng ing : mProcedureIng.getModuleIng()) {
//                realModuleCostPriceSum += ing.getSubtotal();
//            }
//            manufacture.setId(mProcedureIng.getParentId());
//            manufacture.setRealModuleCostPriceSum(realModuleCostPriceSum);
//            manufacture.setRealLabourCostPriceSum(realLabourCostPriceSum);
//            manufactureMapper.updateByPrimaryKeySelective(manufacture);
            MProcedure mProcedure = new MProcedure();
            mProcedure.setId(mProcedureIng.getMProcedureId());
//            mProcedure.setRealLabourHourAmount(mProcedureIng.getLabourHourAmount());
//            mProcedure.setRealSubtotal(mProcedureIng.getSubtotal());
//            mProcedure.setRealModuleSubtotal(realModuleCostPriceSum);
            mProcedure.setProcedureFinishTag("G004-1");
            procedureMapper.updateByPrimaryKeySelective(mProcedure);
            procedureIngMapper.insertSelective(mProcedureIng);
            for (MProcedureModuleIng ing : mProcedureIng.getModuleIng()) {
                ing.setParentId(mProcedureIng.getId());
                moduleIngMapper.insertSelective(ing);
            }
        } catch (Exception e) {
            return Result.error("插入错误");
        }
        return Result.success();
    }

    @Override
    public Result submitOperationService(SubmitOperationView toBean) {
        procedureMapper.submitOperationMapper(toBean);
        return Result.success();
    }

}
